package com.liushuo.view;

import com.liushuo.util.ThreadLocalUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RouterUI extends JFrame{
    public RouterUI() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                 | UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
        Font font = new Font("微软雅黑", Font.PLAIN, 20);

        setTitle("口算练习器");

        setBounds(600, 400, 600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        JPanel ModePanel = new JPanel();
        ModePanel.setBackground(Color.CYAN);
        ModePanel.setLayout(new BoxLayout(ModePanel, BoxLayout.Y_AXIS));
        ModePanel.setBorder(BorderFactory.createEmptyBorder(20, 10, 20, 10));


        JLabel ModeLabel = new JLabel("请选择：");
        ModeLabel.setFont(font);
        ModeLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        ModePanel.setBorder(BorderFactory.createEmptyBorder(20, 10, 20, 10));
        ModePanel.add(ModeLabel);
        ModePanel.add(Box.createVerticalStrut(20));

        JButton GeneralExerciseButton = new JButton("练习模式");
        GeneralExerciseButton.setFont(font);
        GeneralExerciseButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        GeneralExerciseButton.addActionListener(new GeneralButtonListener());
        ModePanel.add(GeneralExerciseButton);
        ModePanel.add(Box.createVerticalStrut(20));

        JButton ScoreButton = new JButton("成绩查询");
        ScoreButton.setFont(font);
        ScoreButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        ScoreButton.addActionListener(new ScoreButtonListener());
        ModePanel.add(ScoreButton);
        ModePanel.add(Box.createVerticalStrut(20));

        JButton MistakeButton = new JButton("错题查询");
        MistakeButton.setFont(font);
        MistakeButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        MistakeButton.addActionListener(new MistakeButtonListener());
        ModePanel.add(MistakeButton);
        ModePanel.add(Box.createVerticalStrut(20));

        JButton loginOutButton = new JButton("注销");
        loginOutButton.setFont(font);
        loginOutButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        loginOutButton.addActionListener(new LoginOutButtonListener());
        ModePanel.add(loginOutButton);

        add(ModePanel);
        setVisible(true);
    }


    private class GeneralButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            dispose();
            new GeneralUI();
        }
    }

    private class ScoreButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            dispose();
            new ScoreUI();
        }
    }

    private class MistakeButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            dispose();
            new MistakeUI();
        }
    }

    private class LoginOutButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            int result = JOptionPane.showConfirmDialog(RouterUI.this, "确定要退出吗？", "提示", JOptionPane.YES_NO_OPTION);
            if (result == JOptionPane.YES_OPTION) {
                dispose();
                LoginRegisterUI loginRegisterUI = new LoginRegisterUI();
                loginRegisterUI.setVisible(true);
                ThreadLocalUtil.removeUserId();
            } else if (result == JOptionPane.NO_OPTION) {
                return;
            }
        }
    }

//    public static void main(String[] args) {
//        new RouterUI();
//    }
}
